home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GSDEVICE.C < prev    next >
C/C++ Source or Header  |  1992-03-04  |  16KB  |  543 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsdevice.c */
  21. /* Device operators for Ghostscript library */
  22. #include "math_.h"            /* for fabs */
  23. #include "memory_.h"            /* for memcpy */
  24. #include "gx.h"
  25. #include "gserrors.h"
  26. #include "gsprops.h"
  27. #include "gsutil.h"
  28. #include "gxarith.h"
  29. #include "gxfixed.h"            /* ditto */
  30. #include "gxmatrix.h"            /* for gzstate.h */
  31. #include "gzstate.h"
  32. #include "gzdevice.h"
  33. #include "gxdevmem.h"
  34.  
  35. /* Import the device list from gdevs.c */
  36. extern gx_device *gx_device_list[];
  37.  
  38. /* Device definitions */
  39. /* Following defines the null device */
  40. private dev_proc_fill_rectangle(null_fill_rectangle);
  41. private dev_proc_tile_rectangle(null_tile_rectangle);
  42. private dev_proc_copy_mono(null_copy_mono);
  43. private dev_proc_draw_line(null_draw_line);
  44.  
  45. private gx_device_procs null_procs = {
  46.     gx_default_open_device,
  47.     gx_default_get_initial_matrix,
  48.     gx_default_sync_output,
  49.     gx_default_output_page,
  50.     gx_default_close_device,
  51.     gx_default_map_rgb_color,
  52.     gx_default_map_color_rgb,
  53.     null_fill_rectangle,
  54.     null_tile_rectangle,
  55.     null_copy_mono,
  56.     gx_default_copy_color,
  57.     null_draw_line,
  58.     gx_default_get_bits,
  59.     gx_default_get_props,
  60.     gx_default_put_props
  61. };
  62. private gx_device null_device = {
  63.     sizeof(device),
  64.     &null_procs,
  65.     "null",
  66.     0, 0,
  67.     72, 72,
  68.     no_margins,
  69.     dci_black_and_white,
  70.     1
  71. };
  72.  
  73. /* The null device */
  74. gx_device *gx_device_null_p = &null_device;
  75.  
  76. /* Flush buffered output to the device */
  77. int
  78. gs_flushpage(gs_state *pgs)
  79. {    gx_device *dev = pgs->device->info;
  80.     return (*dev->procs->sync_output)(dev);
  81. }
  82.  
  83. /* Make the device output the accumulated page description */
  84. int
  85. gs_copypage(gs_state *pgs)
  86. {    return gs_output_page(pgs, 1, 0);
  87. }
  88. int
  89. gs_output_page(gs_state *pgs, int num_copies, int flush)
  90. {    gx_device *dev = pgs->device->info;
  91.     return (*dev->procs->output_page)(dev, num_copies, flush);
  92. }
  93.  
  94. /* Copy scan lines from an image device */
  95. int
  96. gs_copyscanlines(gx_device *dev, int start_y, byte *data, uint size,
  97.   int *plines_copied, uint *pbytes_copied)
  98. {    uint line_size = gx_device_bytes_per_scan_line(dev, 0);
  99.     int code = (*dev->procs->get_bits)(dev, start_y, data, size, 0);
  100.     uint count;
  101.     if ( code < 0 ) return_error(gs_error_undefined);
  102.     count = size / line_size;
  103.     if ( plines_copied != NULL )
  104.       *plines_copied = count;
  105.     if ( pbytes_copied != NULL )
  106.       *pbytes_copied = count * line_size;
  107.     return 0;
  108. }
  109.  
  110. /* Get the current device from the graphics state */
  111. gx_device *
  112. gs_currentdevice(gs_state *pgs)
  113. {    return pgs->device->info;
  114. }
  115.  
  116. /* Get the name of a device */
  117. char *
  118. gs_devicename(gx_device *dev)
  119. {    return dev->dname;
  120. }
  121.  
  122. /* Get the initial matrix of a device. */
  123. void
  124. gs_deviceinitialmatrix(gx_device *dev, gs_matrix *pmat)
  125. {    (*dev->procs->get_initial_matrix)(dev, pmat);
  126. }
  127.  
  128. /* Get the N'th device from the known device list */
  129. gx_device *
  130. gs_getdevice(int index)
  131. {    int i;
  132.     for ( i = 0; gx_device_list[i] != 0; i++ )
  133.        {    if ( i == index ) return gx_device_list[i];
  134.        }
  135.     return 0;            /* index out of range */
  136. }
  137.  
  138. /* Cloning an existing device. */
  139. int
  140. gs_copydevice(gx_device **pnew_dev, gx_device *dev, proc_alloc_t palloc)
  141. {    register gx_device *new_dev;
  142.     new_dev = (gx_device *)(*palloc)(1, dev->params_size, "gs_copydevice");
  143.     if ( new_dev == 0 ) return_error(gs_error_VMerror);
  144.     memcpy(new_dev, dev, dev->params_size);
  145.     new_dev->is_open = 0;
  146.     *pnew_dev = new_dev;
  147.     return 0;
  148. }
  149.  
  150. /* Make a memory (image) device. */
  151. /* If num_colors = -16, -24, or -32, this is a true-color device; */
  152. /* otherwise, num_colors is the number of elements in the palette */
  153. /* (2^N or 3*2^N). */
  154. int
  155. gs_makeimagedevice(gx_device **pnew_dev, gs_matrix *pmat,
  156.   uint width, uint height, byte *colors, int num_colors, proc_alloc_t palloc)
  157. {    gx_device_memory *old_dev;
  158.     register gx_device_memory *new_dev;
  159.     byte *bits;
  160.     int palette_size = num_colors;
  161.     int bpp = 1;
  162.     int pcount;
  163.     int bits_per_pixel;
  164.     float x_pixels_per_unit, y_pixels_per_unit;
  165.     ulong bitmap_size;
  166.     byte palette[256 * 3];
  167.     int has_color;
  168.     if ( width <= 0 || height <= 0 ) return_error(gs_error_rangecheck);
  169.     switch ( num_colors )
  170.        {
  171.     case 3*2:
  172.         palette_size = 2; bpp = 3;
  173.     case 2:
  174.         bits_per_pixel = 1; break;
  175.     case 3*4:
  176.         palette_size = 4; bpp = 3;
  177.     case 4:
  178.         bits_per_pixel = 2; break;
  179.     case 3*16:
  180.         palette_size = 16; bpp = 3;
  181.     case 16:
  182.         bits_per_pixel = 4; break;
  183.     case 3*256:
  184.         palette_size = 256; bpp = 3;
  185.     case 256:
  186.         bits_per_pixel = 8; break;
  187.     case -16:
  188.         bits_per_pixel = 16; palette_size = 0; break;
  189.     case -24:
  190.         bits_per_pixel = 24; palette_size = 0; break;
  191.     case -32:
  192.         bits_per_pixel = 32; palette_size = 0; break;
  193.     default:
  194.         return_error(gs_error_rangecheck);
  195.        }
  196.     old_dev = gdev_mem_device_for_bits(bits_per_pixel);
  197.     if ( old_dev == 0 )        /* no suitable device */
  198.         return_error(gs_error_rangecheck);
  199.     pcount = palette_size * 3;
  200.     /* Check to make sure the palette contains white and black, */
  201.     /* and, if it has any colors, the six primaries. */
  202.     if ( bits_per_pixel <= 8 )
  203.        {    byte *p, *q;
  204.         int primary_mask = 0;
  205.         int i;
  206.         has_color = 0;
  207.         for ( i = 0, p = colors, q = palette;
  208.               i < palette_size; i++, q += 3
  209.             )
  210.            {    int mask = 1;
  211.             switch ( bpp )
  212.                {
  213.             case 1:            /* gray */
  214.                 q[0] = q[1] = q[2] = *p++;
  215.                 break;
  216.             default:        /* bpp == 3, colored */
  217.                 q[0] = p[0], q[1] = p[1], q[2] = p[2];
  218.                 p += 3;
  219.                }
  220. #define shift_mask(b,n)\
  221.   switch ( b ) { case 0xff: mask <<= n; case 0: break; default: mask = 0; }
  222.             shift_mask(q[0], 4);
  223.             shift_mask(q[1], 2);
  224.             shift_mask(q[2], 1);
  225. #undef shift_mask
  226.             primary_mask |= mask;
  227.             if ( q[0] != q[1] || q[0] != q[2] )
  228.                 has_color = 1;
  229.            }
  230.         switch ( primary_mask )
  231.            {
  232.         case 129:        /* just black and white */
  233.             if ( has_color )    /* color but no primaries */
  234.                 return_error(gs_error_rangecheck);
  235.         case 255:        /* full color */
  236.             break;
  237.         default:
  238.             return_error(gs_error_rangecheck);
  239.            }
  240.        }
  241.     else
  242.         has_color = 1;
  243.     /*
  244.      * The initial transformation matrix must map 1 user unit to
  245.      * 1/72".  Let W and H be the width and height in pixels, and
  246.      * assume the initial matrix is of the form [A 0 0 B X Y].
  247.      * Then the size of the image in user units is (W/|A|,H/|B|),
  248.      * hence the size in inches is ((W/|A|)/72,(H/|B|)/72), so
  249.      * the number of pixels per inch is
  250.      * (W/((W/|A|)/72),H/((H/|B|)/72)), or (|A|*72,|B|*72).
  251.      * Similarly, if the initial matrix is [0 A B 0 X Y] for a 90
  252.      * or 270 degree rotation, the size of the image in user
  253.      * units is (W/|B|,H/|A|), so the pixels per inch are
  254.      * (|B|*72,|A|*72).  We forbid non-orthogonal transformation
  255.      * matrices.
  256.      */
  257.     if ( is_fzero2(pmat->xy, pmat->yx) )
  258.         x_pixels_per_unit = pmat->xx, y_pixels_per_unit = pmat->yy;
  259.     else if ( is_fzero2(pmat->xx, pmat->yy) )
  260.         x_pixels_per_unit = pmat->yx, y_pixels_per_unit = pmat->xy;
  261.     else
  262.         return_error(gs_error_undefinedresult);
  263.     /* All checks done, allocate the device. */
  264.     new_dev = (gx_device_memory *)(*palloc)(1, old_dev->params_size, "gs_makeimagedevice(device)");
  265.     if ( new_dev == 0 ) return_error(gs_error_VMerror);
  266.     *new_dev = *old_dev;
  267.     new_dev->initial_matrix = *pmat;
  268.     new_dev->width = width;
  269.     new_dev->height = height;
  270.     new_dev->x_pixels_per_inch = fabs(x_pixels_per_unit) * 72;
  271.     new_dev->y_pixels_per_inch = fabs(y_pixels_per_unit) * 72;
  272.     if ( !has_color )
  273.         new_dev->color_info.max_rgb = 0,
  274.         new_dev->color_info.dither_rgb = 0;
  275.     bitmap_size = gdev_mem_bitmap_size(new_dev);
  276.     if ( bitmap_size > max_uint )    /* can't allocate it! */
  277.         return_error(gs_error_limitcheck);
  278.     bits = (byte *)(*palloc)(1, (uint)bitmap_size + pcount,
  279.                  "gs_makeimagedevice(bits)");
  280.     if ( bits == 0 ) return_error(gs_error_VMerror);
  281.     new_dev->base = bits;
  282.     new_dev->invert = (palette[0] | palette[1] | palette[2] ? -1 : 0);    /* bogus */
  283.     new_dev->palette_size = palette_size;
  284.     new_dev->palette = bits + bitmap_size;
  285.     memcpy(new_dev->palette, palette, pcount);
  286.     new_dev->is_open = 0;
  287.     *pnew_dev = (gx_device *)new_dev;
  288.     return 0;
  289. }
  290.  
  291. /* Set the device in the graphics state */
  292. int
  293. gs_setdevice(gs_state *pgs, gx_device *dev)
  294. {    register device *pdev = pgs->device;
  295.     int was_open = dev->is_open;
  296.     int code;
  297.     /* Initialize the device */
  298.     if ( !was_open )
  299.        {    code = (*dev->procs->open_device)(dev);
  300.         if ( code < 0 ) return_error(code);
  301.         dev->is_open = 1;
  302.        }
  303.     /* Compute device white and black codes */
  304.     pdev->black = (*dev->procs->map_rgb_color)(dev, 0, 0, 0);
  305.     pdev->white = (*dev->procs->map_rgb_color)(dev, gx_max_color_value, gx_max_color_value, gx_max_color_value);
  306.     pdev->info = dev;
  307.     if (    (code = gs_initmatrix(pgs)) < 0 ||
  308.         (code = gs_initclip(pgs)) < 0
  309.        )
  310.         return code;
  311.     if ( !was_open )
  312.         if ( (code = gs_erasepage(pgs)) < 0 )
  313.             return code;
  314.     return gx_remap_color(pgs);
  315. }
  316.  
  317. /* Select the null device.  This is just a convenience. */
  318. void
  319. gs_nulldevice(gs_state *pgs)
  320. {    gs_setdevice(pgs, gx_device_null_p);
  321. }
  322.  
  323. /* Close a device.  The client is responsible for ensuring that */
  324. /* this device is not current in any graphics state. */
  325. int
  326. gs_closedevice(gx_device *dev)
  327. {    int code = 0;
  328.     if ( dev->is_open )
  329.        {    code = (*dev->procs->close_device)(dev);
  330.         if ( code < 0 ) return_error(code);
  331.         dev->is_open = 0;
  332.        }
  333.     return code;
  334. }
  335.  
  336. /* Install enough of a null device to suppress graphics output */
  337. /* during the execution of stringwidth. */
  338. void
  339. gx_device_no_output(gs_state *pgs)
  340. {    pgs->device->info = &null_device;
  341. }
  342.  
  343. /* Read the native color space of the current device. */
  344. gs_color_space
  345. gx_device_color_space(gs_state *pgs)
  346. {    return (gx_device_has_color(pgs->device->info) ?
  347.         gs_color_space_DeviceRGB : gs_color_space_DeviceGray);
  348. }
  349.  
  350. /* Just set the device without reinitializing. */
  351. /* (For internal use only.) */
  352. void
  353. gx_set_device_only(gs_state *pgs, gx_device *dev)
  354. {    pgs->device->info = dev;
  355. }
  356.  
  357. /* Compute the size of one scan line for a device, */
  358. /* with or without padding to a word boundary. */
  359. uint
  360. gx_device_bytes_per_scan_line(gx_device *dev, int pad)
  361. {    ulong bits = (ulong)dev->width * dev->color_info.depth;
  362.     return (pad ?
  363.         (uint)((bits + 31) >> 5) << 2 :
  364.         (uint)((bits + 7) >> 3));
  365. }
  366.  
  367. /* ------ The null `device' ------ */
  368.  
  369. private int
  370. null_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  371.   gx_color_index color)
  372. {    return 0;
  373. }
  374. private int
  375. null_tile_rectangle(gx_device *dev, gx_bitmap *tile,
  376.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one,
  377.   int px, int py)
  378. {    return 0;
  379. }
  380. private int
  381. null_copy_mono(gx_device *dev, byte *data,
  382.   int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  383.   gx_color_index zero, gx_color_index one)
  384. {    return 0;
  385. }
  386. private int
  387. null_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  388.   gx_color_index color)
  389. {    return 0;
  390. }
  391.  
  392. /* ------ Default device procedures ------ */
  393.  
  394. int
  395. gx_default_open_device(gx_device *dev)
  396. {    return 0;
  397. }
  398.  
  399. void
  400. gx_default_get_initial_matrix(register gx_device *dev, register gs_matrix *pmat)
  401. {    pmat->xx = dev->x_pixels_per_inch / 72.0;
  402.     pmat->xy = 0;
  403.     pmat->yx = 0;
  404.     pmat->yy = dev->y_pixels_per_inch / -72.0;
  405.     pmat->tx = 0;
  406.     pmat->ty = dev->height;    /****** WRONG for devices with ******/
  407.                 /****** arbitrary initial matrix ******/
  408. }
  409.  
  410. int
  411. gx_default_sync_output(gx_device *dev)
  412. {    return 0;
  413. }
  414.  
  415. int
  416. gx_default_output_page(gx_device *dev, int num_copies, int flush)
  417. {    return (*dev->procs->sync_output)(dev);
  418. }
  419.  
  420. int
  421. gx_default_close_device(gx_device *dev)
  422. {    return 0;
  423. }
  424.  
  425. gx_color_index
  426. gx_default_map_rgb_color(gx_device *dev,
  427.   gx_color_value r, gx_color_value g, gx_color_value b)
  428. {    /* Map values >= 1/2 to 1, < 1/2 to 0. */
  429.     return ((r | g | b) > gx_max_color_value / 2 ?
  430.         (gx_color_index)1 : (gx_color_index)0);
  431. }
  432.  
  433. int
  434. gx_default_map_color_rgb(gx_device *dev, gx_color_index color,
  435.   gx_color_value prgb[3])
  436. {    /* Map 1 to max_value, 0 to 0. */
  437.     prgb[0] = prgb[1] = prgb[2] = -(gx_color_value)color;
  438.     return 0;
  439. }
  440.  
  441. int
  442. gx_default_copy_color(gx_device *dev, unsigned char *data,
  443.   int data_x, int raster, gx_bitmap_id id,
  444.   int x, int y, int width, int height)
  445. {    return (*dev->procs->copy_mono)(dev, data, data_x, raster, id,
  446.         x, y, width, height, (gx_color_index)0, (gx_color_index)1);
  447. }
  448.  
  449. int
  450. gx_default_get_bits(gx_device *dev, int y,
  451.   unsigned char *data, unsigned int size, int pad)
  452. {    return -1;
  453. }
  454.  
  455. /* Standard device properties */
  456.  
  457. private gs_prop_item props_std[] = {
  458.     prop_def("HWResolution", prt_float_array),
  459.     prop_def("HWSize", prt_int_array),
  460.         /* Following cannot be set yet */
  461.     prop_def("InitialMatrix", prt_float_array),
  462.         /* Following cannot be set */
  463.     prop_def("Name", prt_string),
  464.         /* Slots for arrays */
  465.     prop_float, prop_float,
  466.     prop_int, prop_int,
  467.     prop_float, prop_float, prop_float, prop_float,
  468.       prop_float, prop_float
  469. };
  470.  
  471. /* Get standard properties */
  472. int
  473. gx_default_get_props(register gx_device *dev, register gs_prop_item *plist)
  474. {    if ( plist != 0 )
  475.        {    register gs_prop_item *pi;
  476.         gs_matrix mat;
  477.         memcpy(plist, props_std, sizeof(props_std));
  478.         plist[0].value.a.size = 2;
  479.         plist[1].value.a.size = 2;
  480.         plist[2].value.a.size = 6;
  481.         plist[3].value.a.p.s = dev->dname;
  482.         plist[3].value.a.size = -1;
  483.         pi = &plist[4];
  484.             /* resolution array */
  485.         plist[0].value.a.p.v = pi;
  486.         pi[0].value.f = dev->x_pixels_per_inch;
  487.         pi[1].value.f = dev->y_pixels_per_inch;
  488.         pi += 2;
  489.             /* width/height array */
  490.         plist[1].value.a.p.v = pi;
  491.         pi[0].value.i = dev->width;
  492.         pi[1].value.i = dev->height;
  493.         pi += 2;
  494.             /* matrix */
  495.         plist[2].value.a.p.v = pi;
  496.         (*dev->procs->get_initial_matrix)(dev, &mat);
  497.         pi[0].value.f = mat.xx;
  498.         pi[1].value.f = mat.xy;
  499.         pi[2].value.f = mat.yx;
  500.         pi[3].value.f = mat.yy;
  501.         pi[4].value.f = mat.tx;
  502.         pi[5].value.f = mat.ty;
  503.         pi += 6;
  504.        }
  505.     return sizeof(props_std) / sizeof(gs_prop_item);
  506. }
  507.  
  508. /* Set standard properties */
  509. int
  510. gx_default_put_props(gx_device *dev, gs_prop_item *plist, int count)
  511. {    gs_prop_item *known[2];
  512.     props_extract(plist, count, props_std, 2, known, 1);
  513.     if ( known[0] != 0 )
  514.        {    if ( known[0]->value.a.size != 2 )
  515.             known[0]->status = pv_typecheck;
  516.         else
  517.            {    gs_prop_item *ap = known[0]->value.a.p.v;
  518.             if ( ap[0].value.f <= 0 || ap[1].value.f <= 0 )
  519.                 known[0]->status = pv_rangecheck;
  520.             else
  521.                {    dev->x_pixels_per_inch = ap[0].value.f;
  522.                 dev->y_pixels_per_inch = ap[1].value.f;
  523.                }
  524.            }
  525.        }
  526.     if ( known[1] != 0 )
  527.        {    if ( known[1]->value.a.size != 2 )
  528.             known[1]->status = pv_typecheck;
  529.         else
  530.            {    gs_prop_item *ap = known[1]->value.a.p.v;
  531.             if ( ap[0].value.i <= 0 || ap[0].value.i > 0x7fff ||
  532.                  ap[1].value.i <= 0 || ap[1].value.i > 0x7fff
  533.                )
  534.                 known[1]->status = pv_rangecheck;
  535.             else
  536.                {    dev->width = ap[0].value.i;
  537.                 dev->height = ap[1].value.i;
  538.                }
  539.            }
  540.        }
  541.     return 0;
  542. }
  543.